package com.bill.gaty.mfidelity.activities.listeners;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;

public class TabListener<T extends Fragment> implements android.app.ActionBar.TabListener {
	private Fragment mFragment;
    private final Activity mActivity;
    private final String mTag;
    private final Class<T> mClass;
    public TabListener(Activity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
        
    }

	 
	@SuppressLint("NewApi")
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		// Check if the fragment is already initialized
		if(null!=ft){
        if (mFragment == null) {
            // If not, instantiate and add it to the activity
            mFragment = Fragment.instantiate(mActivity, mClass.getName());
           ft.add(android.R.id.content, mFragment, mTag);
           
             
        } else {
            // If it exists, simply attach it in order to show it
            ft.attach(mFragment);
            
        }
		}

	}

	@SuppressLint("NewApi")
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		 if (mFragment != null&&null!=ft) {
	            // Detach the fragment, because another one is being attached
	            ft.detach(mFragment);
	        }

	}

	 
	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
     // Toast.makeText(null, arg0.getTag().toString(), Toast.LENGTH_LONG);
	}

}
